// pages/unitCheck/unitCheck.js
import { getPatrolTaskPlan, clickStart } from "@/api/unitApi";
import getRole from "@/utils/getRoleType"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    option1: [
      { text: "全部任务状态", value: 0 },
      { text: "未开始", value: "WKS" },
      { text: "进行中", value: "JXZ" },
      { text: "已结束", value: "YJS" },
      { text: "已过期", value: "YGQ" },
      { text: "延期开始", value: "YQKS" },
    ],
    value1: 0,
    option2: [
      { text: "是否上报工单", value: 0 },
      { text: "是", value: "Y" },
      { text: "否", value: "N" },
    ],
    value2: 0,
    unitList: [],

    fightOption: [
      { text: "督察结果", value: 0 },
      { text: "无", value: "null" },
      { text: "合格", value: "Y" },
      { text: "不合格", value: "N" },
    ],
    fightValue: 0,

    type: 'DWZC',
    searchText: '',

    roleType: ''
  },


  // 跳转任务详情
  goOrderDetail(e) {
    let objId = e.currentTarget.dataset.objid;
    let type = e.currentTarget.dataset.type;
    let index = e.currentTarget.dataset.index;
    // wx.navigateTo({
    //   url: `/pages/taskDetail/taskDetail?obj_id=${objId}`,
    // });
    if (type == "WKS") {
      if (this.data.unitList[index].cando == "Y") {
        wx.showModal({
          title: "提示",
          content: "是否开始任务",
          success(res) {
            if (res.confirm) {
              clickStart({ patrolTaskPlanOid: objId }).then((res) => {
                if (res.code == 20000) {
                  wx.navigateTo({
                    url: `/pages/taskDetail/taskDetail?obj_id=${objId}`,
                  });
                }
              });
            } else if (res.cancel) {
              console.log("取消 --------");
            }
          },
        });
      } else if (this.data.unitList[index].onlyView == "Y") {
        wx.navigateTo({
          url: `/pages/taskDetail/taskDetail?obj_id=${objId}`,
        });
      }
    } else {
      wx.navigateTo({
        url: `/pages/taskDetail/taskDetail?obj_id=${objId}`,
      });
    }
  },

  typeChange1({ detail }) {
    this.setData({ value1: detail });
    this.changeTypeFun();
  },

  typeChange2({ detail }) {
    this.setData({ value2: detail });
    this.changeTypeFun();
  },

  // 搜索 提交
  searchSubmit(e) {
    // console.log(e);
    // console.log(this.data.searchText);
    this.collectParams()
  },

  // tab 更改 网格和消防
  onTabsClick(event) {
    console.log('点击tabs', event.detail.value);
    this.setData({ type: event.detail.value })
    this.collectParams()
  },

  // 网格 任务状态更改
  gridTypeChange({ detail }) {
    this.setData({ value1: detail });
    this.collectParams()
  },

  // 督察 下拉更改
  gridDropChange({ detail }) {
    console.log('督察下拉');
    this.setData({ fightValue: detail });
    this.collectParams()
  },

  // 收集查询参数
  collectParams() {
    let { type, value1, searchText, fightValue } = this.data
    let obj = {}
    if (value1 == 0) {
      value1 = null
    }
    if (!searchText) {
      searchText = null
    }
    if (fightValue == 0) {
      fightValue = null
    }
    obj.planType = type
    obj.patrolStatus = value1
    obj.joinUnitOrgName = searchText
    obj.passFlage = fightValue
    const filteredObj = Object.fromEntries(
      Object.entries(obj).filter(([key, value]) => value !== null)
    );
    console.log(filteredObj);
    getPatrolTaskPlan(filteredObj).then((res) => {
      this.setData({ unitList: res.data });
    })
  },



  // 单位自查 查询参数
  changeTypeFun() {
    const { value1, value2 } = this.data;
    let obj = {};
    if (value1 && value2) {
      obj = { patrolStatus: value1, insertWorkSystemFlage: value2 };
    } else if (value1 == 0 && value2 != 0) {
      obj = { insertWorkSystemFlage: value2 };
    } else if (value1 != 0 && value2 == 0) {
      obj = { patrolStatus: value1 };
    }
    obj.planType = 'DWZC'
    console.log(obj);
    getPatrolTaskPlan(obj).then((res) => {
      this.setData({ unitList: res.data });
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 单位自查 工单列表
    // getPatrolTaskPlan().then((res) => {
    //   console.log(res);
    //   this.setData({ unitList: res.data });
    // });

    // wx.setNavigationBarTitle({
    //   title: '网格巡查',
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === "function" && this.getTabBar()) {
      this.getTabBar().setData({
        current: 0, //这个数字是当前页面在tabBar中list数组的索引
      });
    }

    let roleType = getRole()
    this.setData({ roleType })
    if (roleType == 'unit') {
      // 单位自查 工单列表
      getPatrolTaskPlan({ planType: 'DWZC' }).then((res) => {
        this.setData({ unitList: res.data });
      });
    } else if (roleType == 'gridding') { // 网格
      // 网格巡查 工单列表
      getPatrolTaskPlan({ planType: 'DWZC' }).then((res) => {
        this.setData({ unitList: res.data });
      });
    } else if (roleType == 'fireFighting') { // 消防
      // 消防督察 工单列表
      getPatrolTaskPlan({ planType: 'DWZC' }).then((res) => {
        this.setData({ unitList: res.data });
      });
    }


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
});
